/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.shoppingsystem.data;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author victor
 */
public final class AlmacenDatos {

    private static Connection con = generarConexion(); //Por motimos de desarrollo se asigna directamente
    private static EntityManagerFactory emf = generarEMF(); //Por motimos de desarrollo se asigna directamente
    
    //TODO: implementar el uso de estar funciones
    public static void iniciar() {
        con = generarConexion();
        emf = generarEMF();
    }

    public static void terminar() throws SQLException {
        if (con != null) {
            con.close();
        }

        if (emf != null) {
            emf.close();
        }

    }
    
    public static Connection obtenerConexionJDBC(){
        return con;
    }

    public static EntityManager crearNuevoEntityManager() {
        return emf.createEntityManager();
    }

    private static Connection generarConexion() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(AlmacenDatos.class.getName()).log(Level.SEVERE, null, ex);
        }

        try {
            return DriverManager.getConnection(
                    "jdbc:mysql://localhost:3308/shopping_system", "root", "h0m3r0");
            //"jdbc:mysql://10.0.0.2:3306/shopping_system", "root", "univalle");
        } catch (SQLException ex) {
            Logger.getLogger(AlmacenDatos.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    private static EntityManagerFactory generarEMF() {
        return javax.persistence.Persistence.createEntityManagerFactory("ShoppingSystemPU");
    }

}
